package file;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.AbstractTableModel;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileTableModel extends AbstractTableModel {
    private List<File> files = new ArrayList<>();
    private String[] columnNames = {"Name", "Size", "Type", "Date Modified"};

    public void setDirectory(File directory) {
        files.clear();
        if (directory != null && directory.exists() && directory.isDirectory()) {
            File[] fileList = directory.listFiles();
            if (fileList != null) {
                for (File file : fileList) {
                    files.add(file);
                }
            }
        }
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return files.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        File file = files.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return file.getName();
            case 1:
                return file.length() + " bytes";
            case 2:
                return file.isDirectory() ? "Folder" : "File";
            case 3:
                return FileSystemView.getFileSystemView().getSystemTypeDescription(file);
            default:
                return null;
        }
    }

    public File getFileAt(int rowIndex) {
        return files.get(rowIndex);
    }
}